package com.project.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("cookie")
public class CookieController {

    @RequestMapping("test")
    public String testCookie(HttpServletResponse response){
        //创建cookie对象
        Cookie cookie = new Cookie("name", "123");
        cookie.setMaxAge(100);
        //向响应信息中添加cookie信息
        response.addCookie(cookie);
        response.addCookie(new Cookie("age", "12"));
        response.addCookie(new Cookie("tel","34543543543"));

        return "/html/cookie.html";
    }

    @RequestMapping("info")
    @ResponseBody
    public String getInfo(HttpServletRequest request){
        //得到客户端的cookie信息
        Cookie[] cookeArray = request.getCookies();
        String str = "cookie信息：";
        for(Cookie cook : cookeArray){
            str += cook.getName()+":"+cook.getValue()+"<br>";
        }

        return str;
    }

    @RequestMapping("str")
    @ResponseBody
    public String getString(HttpServletResponse response){
        response.addCookie(new Cookie("grade","90"));
        return "str";
    }
}
